package com.minggo.charmword.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;

import com.minggo.charmword.R;
import com.minggo.charmword.model.Plan;
import com.minggo.charmword.service.CharmWordApplication;
import com.minggo.charmword.util.PlanUtil;
import com.minggo.charmword.view.WordDialog;
import com.minggo.charmword.view.WordDialog.OnDialogClickListener;

/**
 * 计划Activity
 * 
 * @author minggo
 * @date 2014-10-22 下午3:43:17
 */
public class PlanActivity extends Activity implements OnClickListener {

	private Button cet4CoreBt;
	private Button cet4allBt;
	private Button sequenceBt;
	private Button converseBt;
	private Button w10Bt;
	private Button w30Bt;
	private Button w50Bt;
	private Button w100Bt;
	private Button planSaveBt;
	private Plan plan;
	private View backV;

	private String from;
	private WordDialog wordDialogExit;
	private WordDialog wordDialogSave;
	private boolean isSaveBt = true;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_plan);

		from = getIntent().getStringExtra("from");

		initUI();
		initData();
		
		CharmWordApplication.allActivities.add(this);
	}

	private void initUI() {
		cet4CoreBt = (Button) findViewById(R.id.bt_cet4_core);
		cet4allBt = (Button) findViewById(R.id.bt_cet4_all);
		sequenceBt = (Button) findViewById(R.id.bt_sequence_order);
		converseBt = (Button) findViewById(R.id.bt_reverse_order);
		w10Bt = (Button) findViewById(R.id.bt_per_count_10);
		w30Bt = (Button) findViewById(R.id.bt_per_count_30);
		w50Bt = (Button) findViewById(R.id.bt_per_count_50);
		w100Bt = (Button) findViewById(R.id.bt_per_count_100);
		planSaveBt = (Button) findViewById(R.id.bt_save_plan);
		backV = findViewById(R.id.lo_plan_back);

		cet4CoreBt.setOnClickListener(this);
		cet4allBt.setOnClickListener(this);
		sequenceBt.setOnClickListener(this);
		converseBt.setOnClickListener(this);
		w10Bt.setOnClickListener(this);
		w30Bt.setOnClickListener(this);
		w50Bt.setOnClickListener(this);
		w100Bt.setOnClickListener(this);
		backV.setOnClickListener(this);
		planSaveBt.setOnClickListener(this);
	}

	private void initData() {
		plan = PlanUtil.getPlan(this);
		if (plan != null) {
			cet4CoreBt.setSelected(plan.wordType == 1 ? true : false);
			cet4allBt.setSelected(plan.wordType == 2 ? true : false);
			sequenceBt.setSelected(plan.sequence == 1 ? true : false);
			converseBt.setSelected(plan.sequence == 2 ? true : false);

			w10Bt.setSelected(plan.percount == 10 ? true : false);
			w30Bt.setSelected(plan.percount == 30 ? true : false);
			w50Bt.setSelected(plan.percount == 50 ? true : false);
			w100Bt.setSelected(plan.percount == 100 ? true : false);

		} else {
			plan = new Plan();

			plan.wordType = 1;
			plan.sequence = 2;
			plan.percount = 30;

			cet4CoreBt.setSelected(true);
			cet4allBt.setSelected(false);
			sequenceBt.setSelected(false);
			converseBt.setSelected(true);

			w10Bt.setSelected(false);
			w30Bt.setSelected(true);
			w50Bt.setSelected(false);
			w100Bt.setSelected(false);
		}
	}

	private void typeSelected(int index) {
		plan.wordType = index;
		cet4CoreBt.setSelected(index == 1 ? true : false);
		cet4allBt.setSelected(index == 2 ? true : false);
	}

	private void sequenceSelected(int index) {

		plan.sequence = index;
		sequenceBt.setSelected(plan.sequence == 1 ? true : false);
		converseBt.setSelected(plan.sequence == 2 ? true : false);
	}

	public void setPerCountSelected(int index) {
		plan.percount = index;
		w10Bt.setSelected(plan.percount == 10 ? true : false);
		w30Bt.setSelected(plan.percount == 30 ? true : false);
		w50Bt.setSelected(plan.percount == 50 ? true : false);
		w100Bt.setSelected(plan.percount == 100 ? true : false);
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.bt_cet4_core:
			typeSelected(1);
			isSaveBt = false;
			break;
		case R.id.bt_cet4_all:
			typeSelected(2);
			isSaveBt = false;
			break;
		case R.id.bt_sequence_order:
			sequenceSelected(1);
			isSaveBt = false;
			break;
		case R.id.bt_reverse_order:
			sequenceSelected(2);
			isSaveBt = false;
			break;
		case R.id.bt_per_count_10:
			setPerCountSelected(10);
			isSaveBt = false;
			break;
		case R.id.bt_per_count_30:
			setPerCountSelected(30);
			isSaveBt = false;
			break;
		case R.id.bt_per_count_50:
			setPerCountSelected(50);
			isSaveBt = false;
			break;
		case R.id.bt_per_count_100:
			setPerCountSelected(100);
			isSaveBt = false;
			break;
		case R.id.lo_plan_back:
			onBackPressed();
			break;
		case R.id.bt_save_plan:
			//isSaveBt = true;
			onBackPressed();
			break;
		default:
			break;
		}
	}

	@Override
	public void onBackPressed() {

		if (from != null) {
			if (from.equals("welcome")) {
				PlanUtil.savePlan(this, plan);
				startActivity(new Intent(this, IndexActivity.class));
			} else if (from.equals("usercenter")) {
				if (isSaveBt) {
					startActivity(new Intent(this, IndexActivity.class));
				} else {
					if (wordDialogExit == null) {
						wordDialogExit = new WordDialog(this, R.style.select_dialog, WordDialog.TWO_BUTTON,
								getString(R.string.dailog_title), getString(R.string.plan_exit_content),
								new OnDialogClickListener() {

									@Override
									public void onDialogClick(int state) {
										switch (state) {
										case WordDialog.BUTTON_LEFT:
											wordDialogExit.dismiss();
											finish();
											overridePendingTransition(R.anim.alpha_in, R.anim.push_right_out);
											break;
										case WordDialog.BUTTON_RIGHT:
											wordDialogExit.dismiss();
											wordDialogSave = new WordDialog(PlanActivity.this, R.style.select_dialog,
													WordDialog.TWO_BUTTON, getString(R.string.dailog_title),
													getString(R.string.plan_save_content), new OnDialogClickListener() {

														@Override
														public void onDialogClick(int state) {
															switch (state) {
															case WordDialog.BUTTON_LEFT:
																wordDialogSave.dismiss();
																finish();
																overridePendingTransition(R.anim.alpha_in, R.anim.push_right_out);
																break;
															case WordDialog.BUTTON_RIGHT:
																PlanUtil.savePlan(PlanActivity.this, plan);
																wordDialogSave.dismiss();
																
																Toast.makeText(PlanActivity.this, "保存成功!", Toast.LENGTH_LONG).show();
																finish();
																overridePendingTransition(R.anim.alpha_in, R.anim.push_right_out);
																break;
															default:
																break;
															}
														}
													});
											wordDialogSave.show();
											break;

										default:
											break;
										}
									}
								});
						wordDialogExit.show();
					}else{
						wordDialogExit.show();
					}
				}
			}
		}

	}
}
